import numpy as np

arr = np.array([41, 42, 43, 44])
x = [True, False, True, False]
newarr = arr[x]
print(newarr)

#Creating the Filter Array
arr = np.array([41, 42, 43, 44])
# Create an empty list
filter_arr = []
# go through each element in arr
for element in arr:
  # if the element is higher than 42, set the value to True, otherwise False:
  if element > 42:
    filter_arr.append(True)
  else:
    filter_arr.append(False)

newarr = arr[filter_arr]
print(filter_arr)
print(newarr)

#Creating Filter Directly From Array
arr = np.array([41, 42, 43, 44])
filter_arr = arr > 42
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)

# Where, condition
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x)
x = np.where(arr%2 == 1)
print(x)

# Search Sorted:The searchsorted() method is assumed to be used on sorted arrays.
arr = np.array([6, 7, 7, 7, 7,8, 9])
x = np.searchsorted(arr, 7)#返回第一个找到的，因为是二叉查找，不一定找到所有的满足条件的情况
print(x)

arr = np.array([6, 7, 7, 7, 7,8, 9])
x = np.searchsorted(arr, 7, side='right') #插入点，会向前或者向后 取决于side的设置，然后返回最佳的插入点。也是二叉查找
print(x)

#找一些位置
arr = np.array([1, 3, 5, 7])
x = np.searchsorted(arr, [2, 4, 6])
print(x)

#插入
arr = np.array([1, 3, 5, 7])
values = [2, 4, 6]
indexes = np.searchsorted(arr, [2, 4, 6])#indexer
newarr = arr
print( newarr.base ) # newarr = arr 居然等价于newarr = arr.copy()
for index, value in sorted(zip(indexes, values), reverse=True): #倒序插入，才能是争取的
    newarr = np.insert(newarr, index, value)
print(arr)
print(newarr)
